package com.xoa.service.h5AppLogin;

import com.alibaba.fastjson.JSONObject;
import com.xoa.dao.WeixinGzh.UserWeixinGzhMapMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.users.Users;
import com.xoa.model.weChatNotice.ModelData;
import com.xoa.model.weChatNotice.ModelMessage;
import com.xoa.model.workflow.FlowRunPrcs;
import com.xoa.model.workflow.TriggerPlugIn;
import com.xoa.util.DateFormat;
import com.xoa.util.dingding.DingDingTool;
import com.xoa.util.enterpriseWeChat.EnterpriseWeChatUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WeixinPublicService {

    @Autowired
    protected UsersMapper usersMapper;

    @Autowired
    protected UserWeixinGzhMapMapper userWeixinGzhMapMapper;

    //转交微信推送提醒
    public String saveWorkWeChatNotice(String uid, TriggerPlugIn triggerPlugIn, String flowStep, FlowRunPrcs fl) {
        //获取accessTokeen
        String result = "";
        String appId = "wxebdebe207cfee590";
        String appSecret = "d95317f1e484e08c2d7fec142571dac6";
        String requestData = "?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
        String accessResult = DingDingTool.dingdingRequestGet(EnterpriseWeChatUrl.WECHAT_NOTICE_GET_TOKEN, requestData);
        String accessToken = "?access_token=" + DingDingTool.getMsgFromJson(accessResult, "access_token");

        //生成模板消息
        ModelMessage message = new ModelMessage();
        message.setTemplate_id("7FESsZ1L8UUsk2HaNy51uiohIn9yc3P3WUbpD5JwdbA");//模板ID
        message.setTopcolor("#173177");
        //根据uid查出byName
        Users users = usersMapper.getUserByUid(Integer.parseInt(uid));
        if(users != null) {
            String byName = users.getByname();
            message.setUrl("http://ymoa.xmhcedu.gov.cn/weixinPublic/weixinNoticeDetail?byName="+byName+"flowId="+fl.getFlowId()+"&flowStep="+flowStep+
                    "&flowPrcs="+fl.getFlowPrcs()+"&tabId="+triggerPlugIn.getTabId()+"&tableName="+triggerPlugIn.getTableName()+"&runId="+fl.getRunId()+"&prcsId="+fl.getPrcsId()+
                    "&opFlag="+fl.getOpFlag());//跳转到待办公文
        }

        //从映射表根据uid查出openId
        String openId = userWeixinGzhMapMapper.getWeixinIdByUid(uid);
        if(openId != null) {
            message.setTouser(openId);
            ModelData data = new ModelData();
            //插入表单传来的数据
            data.getFirst().setValue("您好，为方便对学生进行管理，特通知如下：");
            data.getKeyword1().setValue(triggerPlugIn.getUsers().getCompanyName());
            data.getKeyword2().setValue(triggerPlugIn.getUsers().getUserName());
            data.getKeyword3().setValue(DateFormat.getStrDate(triggerPlugIn.getSendTime()));
            data.getKeyword4().setValue(triggerPlugIn.getRunName());
            data.getRemark().setValue("点击查看详情");
            message.setData(data);
            String postData = JSONObject.toJSONString(message);

            result = DingDingTool.dingdingRequestPost(EnterpriseWeChatUrl.WECHAT_NOTICE_SEND, accessToken, postData);
            result = DingDingTool.getMsgFromJson(result, "errmsg");
        }
        return result;
    }
}
